home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Data 2002 May / CD Rom Data Mayıs 2002.iso / Freeware / Blitz Basic / data1.cab / Support / help / beginners / tutorial / deepend / 4 - complex file example commented.bb < prev    next >
Encoding:
Text File  |  2002-04-10  |  3.2 KB  |  109 lines

  1.  
  2. ; -------------------------------------------------------------
  3. ; Simple file-reading example, with crude string parsing...
  4. ; -------------------------------------------------------------
  5.  
  6. ; We'll just highlight the first occurrence of 'Blitz' in each
  7. ; line of the text file...
  8.  
  9. Graphics 640, 480
  10.  
  11. Color 150, 150, 200
  12. Text 0, 12, "Reading text from 'test.txt'..."
  13.  
  14. ; -------------------------------------------------------------
  15. ; Attempt to read the file, storing the handle as 'info'...
  16. ; -------------------------------------------------------------
  17.  
  18. info = ReadFile ("test.txt")
  19.  
  20.  
  21. ; -------------------------------------------------------------
  22. ; If the handle is valid (non-zero), process the file...
  23. ; -------------------------------------------------------------
  24.  
  25. If info
  26.  
  27.     ; ---------------------------------------------------------
  28.     ; y is the y-position of the text being printed...
  29.     ; ---------------------------------------------------------
  30.  
  31.     y = 24
  32.     
  33.     ; ---------------------------------------------------------
  34.     ; Repeat the line-parsing until end-of-file is reached...
  35.     ; ---------------------------------------------------------
  36.  
  37.     Repeat
  38.  
  39.         ; -----------------------------------------------------
  40.         ; Store a line from the file in a$...
  41.         ; -----------------------------------------------------
  42.  
  43.         a$ = ReadLine (info)
  44.         
  45.         ; -----------------------------------------------------
  46.         ; Print the whole string in yellow...
  47.         ; -----------------------------------------------------
  48.  
  49.         Color 255, 255, 100
  50.         Text 0, y, a$
  51.                 
  52.         ; -----------------------------------------------------
  53.         ; Find 'Blitz', and store the (string) position...
  54.         ; -----------------------------------------------------
  55.  
  56.         pos = Instr (a$, "Blitz")
  57.         
  58.         ; -----------------------------------------------------
  59.         ; If we found a match...
  60.         ; -----------------------------------------------------
  61.  
  62.         If pos
  63.         
  64.             ; -------------------------------------------------
  65.             ; Find the x-position to print Blitz in red...
  66.             ; -------------------------------------------------
  67.  
  68.             x = StringWidth (Left (a$, pos - 1))
  69.             Color 255, 0, 0
  70.             Text x, y, "Blitz"
  71.             
  72.         EndIf
  73.         
  74.         ; -----------------------------------------------------
  75.         ; Increase the text y-position for the next line...
  76.         ; -----------------------------------------------------
  77.  
  78.         y = y + 12
  79.  
  80.  
  81.     ; ---------------------------------------------------------
  82.     ; Repeat above until the next line is true (end-of-file)...
  83.     ; ---------------------------------------------------------
  84.  
  85.     Until Eof (info)
  86.     
  87.     ; ---------------------------------------------------------
  88.     ; Close the file (important!)...
  89.     ; ---------------------------------------------------------
  90.  
  91.     CloseFile info
  92.  
  93. Else
  94.  
  95.     ; ---------------------------------------------------------
  96.     ; If the file handle was invalid (zero)...
  97.     ; ---------------------------------------------------------
  98.  
  99.     RuntimeError "Failed to read test.txt! Aborting..."
  100.  
  101. EndIf
  102.  
  103. ; -------------------------------------------------------------
  104. ; Wait for mouseclick, then quit...
  105. ; -------------------------------------------------------------
  106.  
  107. MouseWait
  108. End
  109.